﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Data.SqlClient;

namespace SoftWareLab_Project
{
    public class InsertTotalWork
    {
        public static object GetDataValue(object value)
        {
            if (value == null)
            {
                return DBNull.Value;
            }

            return value;
        }
        public void Insertss(TWorkEntity WorkEn)
        {
            string strConnString = WebConfigurationManager.ConnectionStrings["DB_EVALUATEConnectionString"].ConnectionString;
            string sqltext = @"insert into TB_TOTALWORK 
                            ([NO_ID]
                            ,[USER_NAME]
                            ,[TERM]
                            ,[YEAR]
                            ,[TEACHING_P]
                            ,[RESEARCH_P]
                            ,[SERVICES_P]
                            ,[OTHER_P])
                           values (@NO_ID,@USER_NAME,@TERM,@YEAR,@TEACHING_P,@RESEARCH_P,@SERVICES_P,@OTHER_P)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(strConnString);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@NO_ID", GetDataValue(WorkEn.NoId));
            sqlcomm.Parameters.AddWithValue("@USER_NAME", GetDataValue(WorkEn.UserName));
            sqlcomm.Parameters.AddWithValue("@TERM", GetDataValue(WorkEn.Term));
            sqlcomm.Parameters.AddWithValue("@YEAR", GetDataValue(WorkEn.Years));
            sqlcomm.Parameters.AddWithValue("@TEACHING_P", GetDataValue(WorkEn.TeachWork));
            sqlcomm.Parameters.AddWithValue("@RESEARCH_P", GetDataValue(WorkEn.ReSerchWork));
            sqlcomm.Parameters.AddWithValue("@SERVICES_P", GetDataValue(WorkEn.ServiceWork));
            sqlcomm.Parameters.AddWithValue("@OTHER_P", GetDataValue(WorkEn.Other));
           


            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }

        public void Update(TWorkEntity WorkEn,int No_id)
        {
            string strConnString = WebConfigurationManager.ConnectionStrings["DB_EVALUATEConnectionString"].ConnectionString;
            string sqltext = @"UPDATE TB_TOTALWORK
                               SET       
                              [TEACHING_P] = @TEACHING_P
                             ,[RESEARCH_P] = @RESEARCH_P
                             ,[SERVICES_P] = @SERVICES_P
                             ,[OTHER_P] = @OTHER_P
                             WHERE NO_ID = "+No_id+"";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(strConnString);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;    
            sqlcomm.Parameters.AddWithValue("@TEACHING_P", GetDataValue(WorkEn.TeachWork));
            sqlcomm.Parameters.AddWithValue("@RESEARCH_P", GetDataValue(WorkEn.ReSerchWork));
            sqlcomm.Parameters.AddWithValue("@SERVICES_P", GetDataValue(WorkEn.ServiceWork));
            sqlcomm.Parameters.AddWithValue("@OTHER_P", GetDataValue(WorkEn.Other));



            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
    }
}